package cn.redragon.soa.common.util;


import static cn.redragon.soa.common.constant.Constants.Separator.DASH;
import static cn.redragon.soa.common.constant.Constants.Separator.UNDERSCORE;

import cn.redragon.soa.common.constant.Constants;
import java.text.NumberFormat;
import java.util.Optional;
import java.util.Random;
import java.util.UUID;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;

@UtilityClass
public class StringUtil {

    public String generateUUID() {
        return UUID.randomUUID().toString().replace(DASH, Constants.EMPTY);
    }

    public String generateUUID(int length) {
        if (length == 32) {
            return generateUUID();
        }
        return RandomStringUtils.randomAlphanumeric(length);
    }

    public String toUnicode(String str) {
        if (StringUtils.isNotBlank(str)) {
            StringBuilder stringBuilder = new StringBuilder();
            str.codePoints().forEach(cp -> {
                String cpHex = Integer.toHexString(cp);
                if (cpHex.length() <= 2) {
                    cpHex = "00" + cpHex;
                }
                stringBuilder.append("\\u").append(cpHex);
            });
            return stringBuilder.toString();
        }
        return str;
    }

    @SneakyThrows
    public Number formatTextAsNumber(String str) {
        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setGroupingUsed(false);
        return numberFormat.parse(str);
    }

    public String formatNumberAsText(Number num) {
        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setGroupingUsed(false);
        return numberFormat.format(num);
    }

    public String concatCacheKey(String... keys) {
        if (ArrayUtils.isEmpty(keys)) {
            return null;
        }

        return StringUtils.joinWith(Constants.Separator.COLON, keys);
    }

    public static String randomString(int len) {
        if (len < 1) {
            return "";
        }
        String base = "ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < len; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public String defaultEmpty(Object value) {
        return Optional.ofNullable(value).map(String::valueOf).orElse(Constants.EMPTY);
    }

    public String joinDash(Object obj1,  Object obj2) {
        return join(obj1, obj2, DASH);
    }

    public String joinUnderscore(Object obj1,  Object obj2) {
        return join(obj1, obj2, UNDERSCORE);
    }

    private String join(Object obj1,  Object obj2, String separator) {
        StringBuffer join = new StringBuffer(String.valueOf(obj1));
        if (obj2 != null) {
            join.append(separator);
            join.append(obj2);
        }
        return join.toString();
    }
}
